<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{

    //进行遍历类别
    static public function getCate($pid){
        //定义一个空数组
        $cate=array();
        //通过传过来的pid查询类别表
        $data=DB::table('cate')->where('pid','=',$pid)->get();
        //遍历查询到的数据
        foreach($data as $k=>$v){
            //通过自身的id去掉用自身的方法(通过pid查询) 判断下面是否还存在子分类 如果有就把他压入自身的一个下标中
            $v['fenlei']=self::getCate($v['cid']);
            //将每次得到的数据遍历到空数组中
            $cate[]=$v;
        } 
         return $cate;
    } 
    //点击logo时进行的操作
    public function getIndex(Request $request){
        //把得到的数据发送到模板进行遍历
        $cate=self::getCate(0);
        // dd($cate);
        //当点击logo时跳转至主页面
        //把销量比较靠前的商品放到热门一栏
        $goods=DB::table('goods')->orderBy('gsold','')->limit(5)->get();
        //拿到所有大类别下面的所有小类别的id
        $cid=array();
        foreach($cate as $k=>$v){
            $cid1=DB::table('cate')->select('cid') ->where('path','like',"%".','.$v['cid'].','."%")->get();

                foreach($cid1 as $kk =>$vv){
                    $cid[$k][] = $v['cid'];
                    $cid[$k][] = $vv['cid'];
                }
            
        }

        //获取第一个大类下面的所有商品
         $goods1=DB::table('goods')->whereIn('cid',$cid[0])->where('gstatus','<',2)->orderBy('gsold','')->get();
        $goods2=DB::table('goods')->whereIn('cid',$cid[1])->where('gstatus','<',2)->orderBy('gsold','')->get();
         $goods3=DB::table('goods')->whereIn('cid',$cid[2])->where('gstatus','<',2)->orderBy('gsold','')->get();
         $goods4=DB::table('goods')->whereIn('cid',$cid[3])->where('gstatus','<',2)->orderBy('gsold','')->get();
         $goods5=DB::table('goods')->whereIn('cid',$cid[4])->where('gstatus','<',2)->orderBy('gsold','')->get();
         $goods6=DB::table('goods')->whereIn('cid',$cid[5])->where('gstatus','<',2)->orderBy('gsold','')->get();
         // $goods2=DB::table('goods')->whereIn('cid',$cid[1])->where('gstatus','<',2)->orderBy('gsold','')->get();
        // dd($cid);
        // dd($goods2);
         $data1 = DB::table('lunbo')->where('lu_status',2)->get();

        // 团购商品列表
        $tuan = DB::table('tuan')
            -> join('goods','tuan.gid','=','goods.gid') 
            -> where('tuan.status','=',1) 
            -> where('tuan.end','>',time()) 
            -> select('tuan.tuid','tuan.gid','goods.gname','goods.gpic','goods.gprice','tuan.nprice','tuan.end') 
            -> limit(6) 
            -> get();


        /*view('/home/layout/layout',['cate'=>$cate,'none'=>'','nav'=>'nav2']);*/
        return view('/home/index/index',['cate'=>$cate,'goods4'=>$goods4,'goods5'=>$goods5,'goods6'=>$goods6,'goods3'=>$goods3,'goods2'=>$goods2,'goods1'=>$goods1,'goods'=>$goods,'data1'=>$data1,'tuan'=>$tuan]);
        //return view('/home/index/index',['cate'=>$cate,'goods1'=>$goods1,'goods'=>$goods,'data1'=>$data1,'tuan'=>$tuan]);
    }


}
